package com.nanfang51g3.ckz.ui.registered;

import android.text.TextUtils;

import com.nanfang51g3.ckz.di.ApiModule;
import com.nanfang51g3.ckz.domain.HttpResultBean;
import com.nanfang51g3.ckz.domain.SuccessBean;
import com.nanfang51g3.ckz.domain.UserBean;
import com.nanfang51g3.ckz.exec.Api2Exception;
import com.nanfang51g3.ckz.http.HttpResultFunc;
import com.nanfang51g3.ckz.http.RestApi;
import com.nanfang51g3.ckz.http.SubscriberInterface;
import com.nanfang51g3.ckz.utils.KLog;
import com.nanfang51g3.ckz.utils.MD5Utils;
import com.nanfang51g3.ckz.utils.SPUtil;
import com.nanfang51g3.ckz.utils.ToolUtils;

import java.util.List;
import java.util.TreeMap;

import javax.inject.Inject;

import retrofit2.Retrofit;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/4/13.
 */
public class RegisteredPresenter implements RegisteredContract.RegisteredActionsListener {

    String message;
    private final RestApi _restApi;
    private final RegisteredContract.View mView;
    @Inject
    Retrofit retrofit;

    @Inject
    public RegisteredPresenter(RestApi restApi, RegisteredContract.View mView) {
        KLog.i("TAG", "进入了...RegisteredPresenter");
        /**
         * @see ApiModule#provideRestApi(Retrofit)
         */
        this._restApi = restApi;
        KLog.i("TAG", _restApi == null ? "进入了...is null" : "进入了...not null");
        /**
         * @see RegisteredPresenterModule#RegisteredPresenterModule(RegisteredContract.View)
         */
        this.mView = mView;
        KLog.i("TAG", mView == null ? "进入了...is null" : "进入了...not null");
    }


    @Override
    public void getRegisteredCode(String phone,final boolean isStart) {
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("phone", phone);
        String sign = MD5Utils.getAccessToken(treeMap);
        if (TextUtils.isEmpty(phone)) {
            mView.showMessage("请输入手机号");
            return;
        }
        if (!ToolUtils.isMobileNO(phone)) {
            mView.showMessage("请输入正确手机号");
            return;
        }
        Observable observable = _restApi.registerSendcode(phone, sign);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new CodeResultFunc<SuccessBean>())
                .subscribe(new SubscriberInterface<SuccessBean>() {
                    @Override
                    public void onCompleted() {
                        KLog.i("Evd", "onCompleted");
                    }

                    @Override
                    public void _onError(String msg) {
                        /**
                         * @see  Registered1Activity#showMessage(String)
                         */
                        mView.showMessage(msg);
                        KLog.i("Evd", "onError " + msg);
                    }

                    @Override
                    public void onNext(SuccessBean o) {
                        KLog.i("Evd", "onNext " + o.toString());
                        /**
                         * @see  Registered1Activity#showMessage(String)
                         */
                        mView.showMessage(message);
                        /**
                         * @see  Registered1Activity#startShowView()
                         */
                        if(isStart){
                            mView.startShowView();
                        }

                    }
                });


    }

    @Override
    public void verificationCode(String code, String phone) {
        if (TextUtils.isEmpty(code)) {
            mView.showMessage("请输入验证码");
            return;
        }
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("code", code);
        treeMap.put("phone", phone);
        String sign = MD5Utils.getAccessToken(treeMap);
        KLog.i("Evd", treeMap.toString() + "-" + sign);
        Observable observable = _restApi.registerVerifycode(code, phone, sign);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new CodeResultFunc<SuccessBean>())
                .subscribe(new SubscriberInterface<SuccessBean>() {
                    @Override
                    public void onCompleted() {
                        KLog.i("Evd", "onCompleted");
                    }

                    @Override
                    public void _onError(String msg) {
                        /**
                         * @see  Registered2Activity#showMessage(String)
                         */
                        mView.showMessage(msg);
                        KLog.i("Evd", "onError " + msg);
                    }

                    @Override
                    public void onNext(SuccessBean o) {
                        KLog.i("Evd", "SuccessBean " + o.toString());
                        /**
                         * @see  Registered2Activity#showMessage(String)
                         */
                        mView.showMessage(message);
                        /**
                         * @see Registered2Activity#startShowView()
                         */
                        mView.startShowView();
                    }
                });


    }

    @Override
    public void register(boolean selectPasswordBoolean, final String phone, String password, String confirmPassword, String rid) {
        KLog.i("TAG", retrofit == null ? "retrofit-1" : "retrofit-0");
        if (TextUtils.isEmpty(password)) {
            /**
             * @see  Registered3Activity#showMessage(String)
             */
            mView.showMessage("请输入密码");
            return;
        }
        if (TextUtils.isEmpty(confirmPassword)) {
            /**
             * @see  Registered3Activity#showMessage(String)
             */
            mView.showMessage("请输入确认密码");
            return;
        }
        if (!password.equals(confirmPassword)) {
            /**
             *@see  Registered3Activity#showMessage(String)
             */
            mView.showMessage("两次密码不一致");
            return;
        }

        if (!selectPasswordBoolean) {
            /**
             * @see  Registered3Activity#showMessage(String)
             */
            mView.showMessage("请选中同意创客推合作协议");
            return;
        }
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("phone", phone);
        treeMap.put("password", password);
        treeMap.put("rid", rid == null ? "8520" : rid);
        String sign = MD5Utils.getAccessToken(treeMap);
        SPUtil.putAndApply(mView.getContext(), "username", phone);
        KLog.i("Evd", sign);
        Observable observable = _restApi.register(phone, password, rid, sign);
        observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new RegisterResultFunc<List<UserBean>>())
                .subscribe(new SubscriberInterface<UserBean>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        mView.showProgress();
                        KLog.i("Evd", "onStart");
                    }

                    @Override
                    public void onCompleted() {
                        mView.dismissProgress();
                        KLog.i("Evd", "onCompleted");
                        /**
                         * @see  Registered3Activity#showProgress()
                         */
                    }

                    @Override
                    public void _onError(String msg) {
                        KLog.i("Evd", "onError " + msg);
                        /**
                         * @see  Registered3Activity#showMessage(String)
                         */
                        mView.showMessage(msg);
                        /**
                         * @see  Registered3Activity#dismissProgress()
                         */
                        mView.dismissProgress();
                    }

                    @Override
                    public void onNext(UserBean o) {
                        KLog.i("Evd", "OnNext " + o.getUsername());
                        SPUtil.putAndApply(mView.getContext(), "phone", phone);
                        SPUtil.putAndApply(mView.getContext(), "userpic", o.getUserpic());
                        /**
                         * @see  Registered3Activity#showMessage(String)
                         */
                        mView.showMessage(message);
                        /**
                         * @see  Registered3Activity#startShowView()
                         */
                        mView.startShowView();
                    }
                });

    }

    class CodeResultFunc<T> extends HttpResultFunc<T> {
        @Override
        public T call(HttpResultBean<T> httpResult) {
            KLog.i("Evd", httpResult == null ? "1-" : "0" + httpResult.getCode() + "-" + httpResult.getInfo());
            if (
                    httpResult.getCode() == 2 ||
                            httpResult.getCode() == 3 ||
                            httpResult.getCode() == 4 ||
                            httpResult.getCode() == 7 ||
                            httpResult.getCode() == 8) {
                throw new Api2Exception(httpResult.getInfo());
            }
            message = httpResult.getInfo();
            return httpResult.getData();
        }
    }

    class RegisterResultFunc<T> extends HttpResultFunc<T> {
        @Override
        public T call(HttpResultBean<T> httpResult) {
            KLog.i("Evd", httpResult == null ? "1-" : "0" + httpResult.getCode() + "-" + httpResult.getInfo());
            if (httpResult.getCode() == 2 || httpResult.getCode() == 3 || httpResult.getCode() == 4) {
                throw new Api2Exception(httpResult.getInfo());
            }
            message = httpResult.getInfo();
            return httpResult.getData();
        }
    }
}
